//=============================================================================
// PlasmaGunChargedProjectile.
//=============================================================================
class PlasmaGunChargedProjectile extends LinkProjectile
	placeable;

#exec OBJ LOAD FILE=XEffectMat.utx
#exec obj load file=..\Sounds\WeaponSounds.uax

var xEmitter Trail1,Trail2;
//var int Links;

var int GoopLevel;
var float GoopVolume;




replication
{
//    unreliable if (bNetInitial && Role == ROLE_Authority)
//        Links;

    reliable if (Role == ROLE_Authority)
	GoopLevel;
}

simulated function Destroyed()
{
    if (Trail1 != None)
        Trail1.Destroy();
    if (Trail2 != None)
        Trail2.Destroy();

	Super.Destroyed();
}

simulated function PostBeginPlay()
{
    local Rotator R;

	Super.PostBeginPlay();

//    if ( EffectIsRelevant(vect(0,0,0),false) )
//		Trail1 = Spawn(class'PlasmaProjEffect',self);
		Trail1 = Spawn(class'PlasmaSparkTrail',self);
		Trail2 = Spawn(class'PlasmaProjEffect',self);

	Velocity = Vector(Rotation);
    Acceleration = Velocity * 3000.0;
    Velocity *= Speed;

    R = Rotation;
    R.Roll = Rand(65536);
    SetRotation(R);

    if ( Level.bDropDetail )
	{
		bDynamicLight = false;
		LightType = LT_None;
	}
}

simulated function LinkAdjust()
{
//    local float ls;
//
//    if (Links > 0)
//    {
//        ls = class'LinkFire'.default.LinkScale[Min(Links,5)];
//
//        SetDrawScale3D(default.DrawScale3D * (ls + 0.5));
//
//    	if ( Trail1 != None )
//        {
//            Trail1.mLifeRange[0] *= (ls + 1);
//            Trail1.mLifeRange[1] = Trail1.mLifeRange[0];
//
//            Trail1.mSizeRange[0] *= (ls + 1);
//            Trail1.mSizeRange[1] = Trail1.mSizeRange[0];
//
//            Trail1.mPosDev.Y *= (ls + 1);
//            Trail1.mPosDev.Z = Trail1.mPosDev.Y;
//
//            Trail1.Skins[0] = Texture'XEffectMat.Link.link_muz_blue';
//        }
//
//        Speed = default.Speed + 200*3;
//        MaxSpeed = default.MaxSpeed + 350*3;
//	    Velocity = Speed * Vector(Rotation);
//
//        SetCollisionSize(ls*10, ls*10);
//
        Skins[0] = FinalBlend'TFMedia.Plasma_Muz_blueFB';
        LightHue = 150;
//    }
}

simulated function SetGoopLevel( int NewGoopLevel )
{
    local float Regen;

    GoopLevel = NewGoopLevel;
    Regen=float(GoopLevel);
    GoopVolume = sqrt(float(GoopLevel));
    SetDrawScale((0.75+(GoopVolume/3.5))*default.DrawScale);
    SetCollisionSize(CollisionRadius*(1+(Regen/50)), CollisionHeight*(1+(Regen/50)));
    //SetDrawScale3D(default.DrawScale3D * ((GoopVolume/2) + 1));
	Speed = default.Speed + 30*GoopLevel;
        MaxSpeed = default.MaxSpeed + 45*GoopLevel;
	Velocity = Speed * Vector(Rotation);
	Damage=5+GoopLevel*GoopLevel;
	MomentumTransfer=default.MomentumTransfer+2500*GoopLevel;

    LightBrightness = default.LightBrightness+2*GoopLevel;
    LightRadius = default.LightRadius + GoopLevel;

    Trail1.SetDrawScale(1+Regen / 20);
    Trail1.mLifeRange[0] += (Regen/75);
    Trail1.mLifeRange[1] += (Regen/75);
}

simulated function PostNetBeginPlay()
{
//    if (Role < ROLE_Authority)
//        LinkAdjust();
	if ( Level.bDropDetail || Level.DetailMode == DM_Low )
		LightType = LT_None;
}

//simulated function Explode(vector HitLocation, vector HitNormal)
//{
//    if ( EffectIsRelevant(Location,false) )
//	{
//        if (Links == 0)
//            Spawn(class'PlasmaProjSparks',,, HitLocation, rotator(HitNormal));
//        else
//            Spawn(class'PlasmaProjSparks',,, HitLocation, rotator(HitNormal));
//	}
//    PlaySound(Sound'WeaponSounds.PulseRifle.PulseRifleAltFire');
//	Destroy();
//}

simulated function ProcessTouch (Actor Other, vector HitLocation)
{
    local Vector X, RefNormal, RefDir;

	if (Other == Instigator) return;
    if (Other == Owner) return;

    if (Other.IsA('xPawn') && xPawn(Other).CheckReflect(HitLocation, RefNormal, Damage*0.25))
    {
        if (Role == ROLE_Authority)
        {
            X = Normal(Velocity);
            RefDir = X - 2.0*RefNormal*(X dot RefNormal);
            //Log("reflecting off"@Other@X@RefDir);
            Spawn(Class, Other,, HitLocation+RefDir*20, Rotator(RefDir));
        }
        Destroy();
    }
    else if ( !Other.IsA('Projectile') || Other.bProjTarget )
	{
		if ( Role == ROLE_Authority )
			Other.TakeDamage(Damage * (1.0 + float(Links)),Instigator,HitLocation,MomentumTransfer * (1.0 + float(Links)*0.25) * Normal(Velocity),MyDamageType);
		Explode(HitLocation, vect(0,0,1));
	}
}

function BlowUp(vector HitLocation)
{
	//HurtRadius(Damage, DamageRadius, MyDamageType, MomentumTransfer, HitLocation );
	HurtRadius(5+Damage, 175+8*GoopLevel, class'PlasmaGunDamType', MomentumTransfer, Location );
        //PlaySound(sound'WeaponSounds.BExplosion3',,2.5*TransientSoundVolume+Damage);
        MakeNoise(1.0);
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
	local RocketMark PlasmaDecal;

	PlaySound(sound'WeaponSounds.BExplosion3',,2.5*TransientSoundVolume);

	Spawn(class'PlasmaProjSparks',,, HitLocation, rotator(HitNormal));

	if (GoopLevel>3)
	    	Spawn(class'RocketSmokeRing',,,HitLocation + HitNormal*16,rotator(HitNormal));
	if (GoopLevel>10)
		Spawn(class'PlasmaWallHitSparks',,,HitLocation + HitNormal*16,rotator(HitNormal) );
	if (GoopLevel>15)
	{
	        Spawn(class'ExplosionCrap',,, HitLocation, rotator(HitNormal));
		PlaySound(sound'WeaponSounds.BExplosion2',,2.5*TransientSoundVolume+Damage);
	}
	if (GoopLevel>20)
		Spawn(class'PlasmaExplosion',,, HitLocation, rotator(HitNormal));

    if ( EffectIsRelevant(Location,false) )
    {
		if ( (ExplosionDecal != None) && (Level.NetMode != NM_DedicatedServer) )
			PlasmaDecal=Spawn(class'PlasmaMark',self,,Location, rotator(-HitNormal));
    }

	BlowUp(HitLocation);
	Destroy();
}

defaultproperties
{
     Speed=2000.000000
     MomentumTransfer=15000.000000
     MyDamageType=Class'Taskforces.PlasmaGunDamType'
     LightHue=150
     LightBrightness=180.000000
     LightRadius=5.000000
     LifeSpan=4.000000
     DrawScale=1.500000
     DrawScale3D=(X=2.550000,Y=1.700000,Z=1.700000)
     Skins(0)=FinalBlend'TFMedia.PlasmaGun.Plasma_Muz_blueFB'
}
